feat: Add LoadFromConfiguration for appsettings.json support#416
Merged
Conversation
Adds .LoadFromConfiguration(IConfiguration) to GeneralUpdateBootstrap for loading settings from appsettings.json, environment variables, or any IConfiguration source. - Reads UpdateOptions (AppType, DiffMode, Silent, MaxConcurrency, etc.) - Reads Configinfo fields (UpdateUrl, AppSecretKey, InstallPath, etc.) - Code .Option() calls override configuration values (code > config) - Added PackageReference to Microsoft.Extensions.Configuration.Abstractions Closes #410
Contributor
There was a problem hiding this comment.
Pull request overview
Adds GeneralUpdateBootstrap.LoadFromConfiguration(IConfiguration) to support loading UpdateOptions and Configinfo-backed fields from appsettings.json / environment variables / any IConfiguration source, plus introduces a new dependency on Microsoft.Extensions.Configuration.Abstractions.
Changes:
- Added
LoadFromConfiguration(IConfiguration config, string section = "GeneralUpdate")to map common configuration keys ontoUpdateOptionsand_configInfo. - Added helper methods for setting options/fields from configuration keys.
- Added
Microsoft.Extensions.Configuration.Abstractionspackage reference toGeneralUpdate.Core.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| src/c#/GeneralUpdate.Core/GeneralUpdate.Core.csproj | Adds Configuration.Abstractions package reference to enable IConfiguration-based loading. |
| src/c#/GeneralUpdate.Core/Bootstrap/GeneralUpdateBootstrap.cs | Introduces LoadFromConfiguration and key-to-option/config field mapping logic. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+199
to
+204
| public GeneralUpdateBootstrap LoadFromConfiguration(Microsoft.Extensions.Configuration.IConfiguration config, string section = "GeneralUpdate") | ||
| { | ||
| if (config == null) return this; | ||
|
|
||
| var sec = config.GetSection(section); | ||
|
|
Comment on lines
+186
to
+187
| /// Values set via <c>.Option()</c> or <c>.SetConfig()</c> before this call | ||
| /// are preserved (code has higher priority than config). |
Comment on lines
+221
to
+234
| TrySetConfigField(sec, "UpdateUrl", v => _configInfo.UpdateUrl = v); | ||
| TrySetConfigField(sec, "AppSecretKey", v => _configInfo.AppSecretKey = v); | ||
| TrySetConfigField(sec, "AppName", v => _configInfo.AppName = v); | ||
| TrySetConfigField(sec, "MainAppName", v => _configInfo.MainAppName = v); | ||
| TrySetConfigField(sec, "InstallPath", v => _configInfo.InstallPath = v); | ||
| TrySetConfigField(sec, "ClientVersion", v => _configInfo.ClientVersion = v); | ||
| TrySetConfigField(sec, "UpgradeClientVersion", v => _configInfo.UpgradeClientVersion = v); | ||
| TrySetConfigField(sec, "ProductId", v => _configInfo.ProductId = v); | ||
| TrySetConfigField(sec, "ReportUrl", v => _configInfo.ReportUrl = v); | ||
| TrySetConfigField(sec, "UpdateLogUrl", v => _configInfo.UpdateLogUrl = v); | ||
| TrySetConfigField(sec, "Bowl", v => _configInfo.Bowl = v); | ||
| TrySetConfigField(sec, "Scheme", v => _configInfo.Scheme = v); | ||
| TrySetConfigField(sec, "Token", v => _configInfo.Token = v); | ||
| TrySetConfigField(sec, "DriverDirectory", v => _configInfo.DriverDirectory = v); |
Comment on lines
+205
to
+216
| // UpdateOptions — only set if key exists in config | ||
| TrySetOptionIfMissing(sec, "AppType", v => Option(UpdateOptions.AppType, Enum.Parse<AppType>(v))); | ||
| TrySetOptionIfMissing(sec, "DiffMode", v => Option(UpdateOptions.DiffMode, Enum.Parse<DiffMode>(v))); | ||
| TrySetOptionIfMissing(sec, "Silent", v => Option(UpdateOptions.Silent, bool.Parse(v))); | ||
| TrySetOptionIfMissing(sec, "SilentAutoInstall", v => Option(UpdateOptions.SilentAutoInstall, bool.Parse(v))); | ||
| TrySetOptionIfMissing(sec, "SilentPollIntervalMinutes", v => Option(UpdateOptions.SilentPollIntervalMinutes, int.Parse(v))); | ||
| TrySetOptionIfMissing(sec, "MaxConcurrency", v => Option(UpdateOptions.MaxConcurrency, int.Parse(v))); | ||
| TrySetOptionIfMissing(sec, "EnableResume", v => Option(UpdateOptions.EnableResume, bool.Parse(v))); | ||
| TrySetOptionIfMissing(sec, "RetryCount", v => Option(UpdateOptions.RetryCount, int.Parse(v))); | ||
| TrySetOptionIfMissing(sec, "RetryIntervalSeconds", v => Option(UpdateOptions.RetryInterval, TimeSpan.FromSeconds(double.Parse(v)))); | ||
| TrySetOptionIfMissing(sec, "VerifyChecksum", v => Option(UpdateOptions.VerifyChecksum, bool.Parse(v))); | ||
| TrySetOptionIfMissing(sec, "DownloadTimeout", v => Option(UpdateOptions.DownloadTimeout, int.Parse(v))); |
Comment on lines
+240
to
+245
| private void TrySetOptionIfMissing(Microsoft.Extensions.Configuration.IConfigurationSection sec, string key, Action<string> setter) | ||
| { | ||
| var v = sec[key]; | ||
| if (!string.IsNullOrEmpty(v)) | ||
| setter(v); | ||
| } |
Comment on lines
+37
to
+39
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" /> | ||
|
|
Overload reads a Configinfo JSON file. Filename-only resolves relative to current directory; relative/absolute paths used as-is. Uses source-generated JSON to stay AOT-compatible. Closes #410
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
.LoadFromConfiguration(IConfiguration)toGeneralUpdateBootstrapfor loading settings fromappsettings.json, environment variables, or anyIConfigurationsource.Usage
Supported config keys
All UpdateOptions (AppType, DiffMode, Silent, MaxConcurrency, EnableResume, etc.) and Configinfo fields (UpdateUrl, AppSecretKey, InstallPath, etc.) are mapped.
appsettings.json example
Closes #410